package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.AttributeMApper;
import cn.tedu.csmall.product.mapper.AttributeTemplateMapper;
import cn.tedu.csmall.product.pojo.dto.AttributeAndNewDTO;
import cn.tedu.csmall.product.pojo.dto.BrandAndNewDTO;
import cn.tedu.csmall.product.pojo.entity.Attribute;

import cn.tedu.csmall.product.pojo.vo.AttributeListitemVO;
import cn.tedu.csmall.product.pojo.vo.AttributeStandardVO;
import cn.tedu.csmall.product.pojo.vo.AttributeTemplateListitemVO;
import cn.tedu.csmall.product.pojo.vo.AttributeTemplateStandardVO;
import cn.tedu.csmall.product.service.IAttributeService;

import cn.tedu.csmall.product.web.JsonResult;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class AttributeServiceImpl implements IAttributeService, ServiceCode {
    @Autowired
    private AttributeMApper attributeMApper;
    @Autowired
    AttributeTemplateMapper attributeTemplateMapper;
    public AttributeServiceImpl() {
        log.debug("创建业务类:AttributeServiceImpl");
    }
    @Override
    public void andNew(AttributeAndNewDTO attributeAndNewDTO) {
        log.debug("开始处理【添加】的业务，参数：{}",attributeAndNewDTO);
        AttributeTemplateStandardVO attributeTemplateMapperById = attributeTemplateMapper.getById(attributeAndNewDTO.getTemplateId());
        if (attributeTemplateMapperById==null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"属性模板必须存在");
        }
        LocalDateTime now=LocalDateTime.now();
        Attribute attribute=new Attribute();
        BeanUtils.copyProperties(attributeAndNewDTO,attribute);
        attribute.setGmtCreate(now);
        attribute.setGmtModified(now);
        log.debug("即将向数据库中插入数据：{}",attribute);
        int row = attributeMApper.insert(attribute);
        if (row!=1){
            String message="添加数据失败！服务器忙，请稍后再一次尝试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }
    @Override
    public void delete(Long id) {
        log.debug("开始处理【删除】的业务，参数{}",id);
        AttributeStandardVO byId = attributeMApper.getById(id);
        log.debug("尝试删除数据库相册数据，相册id：{}，删除数据：{}",id,byId);
        if (byId==null) {
            String message="删除失败，该id:"+id+"不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
        int row= attributeMApper.deleteById(id);
        if(row!=1){
            String message="删除失败！服务器忙，请稍后再一次尝试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }

    }
    @Override
    public List<AttributeTemplateListitemVO> listAttribute() {
        log.debug("开始查询属性数据");
        List<AttributeTemplateListitemVO> attributeTemplateListitemVOS = attributeTemplateMapper.listAttribute();
        if (attributeTemplateListitemVOS.isEmpty()){
            String message="你所查询的数据不存在！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        return attributeTemplateListitemVOS;
    }
    @Override
    public List<AttributeListitemVO> listAttribute(Long templateId) {
        log.debug("更具templateId开始查询属性数据:{}",templateId);
        List<AttributeListitemVO> attributeListitemVOList = attributeMApper.listByAttributeListitem(templateId);
        if (attributeListitemVOList.isEmpty()){
            String message="你所查询的数据不存在！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        return attributeListitemVOList;
    }
    @Override
    public AttributeStandardVO getAttribute(Long id) {
        log.debug("开始查询属性数据");
        AttributeStandardVO byId = attributeMApper.getById(id);
        if (byId==null){
            String message="你所查询的数据不存在！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        return byId;
    }
}
